/*
 * at24c16.c
 *
 *  Created on: 2014-3-11
 *      Author: Rome
 */

#include "at24c16.h"
#include "iic.h"
#include "unistd.h"
#include "check_reset.h"
#include "helper.h"


void AcquireInitialInformation(alt_u8* pbuf)
{
	alt_u32 test = 0;
	// initial gate way ip
	test = GetResetPin();

	if(test != 0)
	{
		At24c16Read(0, pbuf, 18);
		if(*pbuf == 0xFF)
		{
			*pbuf = 192;
			*(pbuf + 1) = 168;
			*(pbuf + 2) = 1;
			*(pbuf + 3) = 1;

			// initial mask
			*(pbuf + 4) = 255;
			*(pbuf + 5) = 255;
			*(pbuf + 6) = 255;
			*(pbuf + 7) = 0;

			// initial mac address
			*(pbuf + 8) = 0;
			*(pbuf + 9) = 1;
			*(pbuf + 10) = 2;
			*(pbuf + 11) = 3;
			*(pbuf + 12) = 4;
			*(pbuf + 13) = 5;

			// initial IP
			*(pbuf + 14) = 192;
			*(pbuf + 15) = 168;
			*(pbuf + 16) = 1;
			*(pbuf + 17) = 200;
		}
	}
	else
	{
		*pbuf = 192;
		*(pbuf + 1) = 168;
		*(pbuf + 2) = 1;
		*(pbuf + 3) = 1;

		// initial mask
		*(pbuf + 4) = 255;
		*(pbuf + 5) = 255;
		*(pbuf + 6) = 255;
		*(pbuf + 7) = 0;

		// initial mac address
		*(pbuf + 8) = 0;
		*(pbuf + 9) = 1;
		*(pbuf + 10) = 2;
		*(pbuf + 11) = 3;
		*(pbuf + 12) = 4;
		*(pbuf + 13) = 5;

		// initial IP
		*(pbuf + 14) = 192;
		*(pbuf + 15) = 168;
		*(pbuf + 16) = 1;
		*(pbuf + 17) = 200;
	}

	return;
}

void At24c16Read(alt_u16 Addr, alt_u8* Pbuf, alt_u16 length)
{
	alt_u32 i;
	for(i = 0; i < length; i ++)
	{
		*(Pbuf + i) = AT24C16_R((unsigned short)(Addr + i));
	}
	return;
}

void At24c16Write(alt_u16 Addr, alt_u8* Pbuf, alt_u16 length)
{
	alt_u32 i;
	alt_u8 WrTemp = 0;
	for(i = 0; i < length; i ++)
	{
		WrTemp = *(Pbuf + i);
		AT24C16_W((unsigned short)(Addr + i), WrTemp);
	}
	return;
}

